<template>
  <el-container>
    <!--查询条件开始-->
    <el-header height="1">
      <el-form ref="queryForm" size="small" :model="parmsValue.query" label-width="160px" class="middle-form">
        <el-row>
          <el-col :span="8">
            <el-form-item prop="ir_tag_name" label="指标名称">
              <el-input v-model="parmsValue.query.ir_tag_name" :clearable="true" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item prop="ir_tag_level" label="级别">
              <el-select v-model="parmsValue.query.ir_tag_level" :clearable="true">
                <el-option v-for="(item,index) in dict.ir_tag_level" :key="index" :value="index" :label="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-button type="success" size="small" @click="query">查询</el-button>
          <el-button type="info" size="small" @click="resetFields('queryForm')">重置</el-button>
          <el-button type="success" size="small" @click="showDialog('add')">新增利率指标</el-button>
        </el-row>
      </el-form>
    </el-header>
    <!--查询条件结束-->
    <!--列表开始-->
    <el-main>
      <table-pagination
        :table-data="table.ir1.tableData"
        :table-header-config="table.tableHeaderConfig"
        :total="table.ir1.total"
        :page-size="table.ir1.limit"
        :now-page="table.ir1.nowPage"
        :dict-data="dict"
        :operation-config="table.operationConfig"
        @table-current-change="showSecondTable"
        @current-change="currentChange"
      />
      <table-pagination
        :table-data="table.ir2.tableData"
        :table-header-config="table.tableHeaderConfig2"
        :total="table.ir2.total"
        :page-size="table.ir2.limit"
        :now-page="table.ir2.nowPage"
        :operation-config="table.operationConfig2"
        @current-change="currentChange2"
      />
    </el-main>
    <!--列表结束-->
    <!--利率指标弹出框开始-->
    <el-dialog :title="editModStr" :visible.sync="parmsValue.updateOpen" :before-close="handleClose">
      <el-form ref="updateForm" size="small" :model="parmsValue.update" label-width="160px" :rules="form.rules" class="middle-form">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="ir_tag_name" label="指标名称">
              <el-input v-model="parmsValue.update.ir_tag_name" :clearable="true" :disabled="detailMod" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="ir_tag_level" label="等级">
              <el-select v-model="parmsValue.update.ir_tag_level" :clearable="true" :disabled="detailMod">
                <el-option v-for="(item,index) in dict.ir_tag_level" :key="index" :value="index" :label="item" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" size="small" @click="closeDialog">取消</el-button>
        <el-button type="success" size="small" @click="updateIrTag">确定</el-button>
      </div>
    </el-dialog>
    <!--利率指标弹框结束，利率行情弹框开始-->
    <el-dialog :title="editModStr2" :visible.sync="parmsValue.updateOpen2" :before-close="handleClose2">
      <el-form ref="updateForm2" size="small" :model="parmsValue.update2" label-width="160px" :rules="form.rules" class="middle-form">
        <el-row>
          <el-col :span="12">
            <el-form-item prop="ir_tradedate" label="生效日期">
              <el-date-picker v-model="parmsValue.update2.ir_tradedate" format="yyyy-MM-dd" value-format="yyyyMMdd" :clearable="true" :disabled="detailMod2" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="ir_tag_code" label="指标编码">
              <el-input v-model="parmsValue.update2.ir_tag_code" :clearable="true" :disabled="true" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item prop="ir_tag_name" label="指标名称">
              <el-input v-model="parmsValue.update2.ir_tag_name" :clearable="true" :disabled="true" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item prop="ir_rate" label="利率(%)">
              <el-input v-model="parmsValue.update2.ir_rate" :clearable="true" :disabled="detailMod2" />
            </el-form-item>
            <el-form-item prop="max_ir_rate">
              <el-input v-model="parmsValue.update2.max_ir_rate" :clearable="true" :disabled="detailMod2" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" size="small" @click="closeDialog2">取消</el-button>
        <el-button v-if="updateMod2" type="success" size="small" @click="updateRateInfo">确定</el-button>
        <el-button v-if="addMod2" type="success" size="small" @click="addNewIrInfo">确定</el-button>
      </div>
    </el-dialog>
    <!--弹出框结束-->
  </el-container>
</template>

<script>
  import { formItems } from '@/utils/formItems'
  import * as kk from '@/utils/k-utils'
  import request from '@/utils/request'
  import axios from 'axios'

  export default {
    data() {
      return {
        dict: {
          ir_tag_level: {}
        },
        table: {
          currentSelect: {},
          ir1: { total: 100, limit: 10, nowPage: 1, exeid: 'getProdNormRate', tableData: [] },
          ir2: { total: 100, limit: 10, nowPage: 1, exeid: 'getNormRateInfo', tableData: [] },
          tableHeaderConfig: [
            formItems.irTagName, formItems.irTagLevel
          ],
          tableHeaderConfig2: [
            formItems.irTradeDate, formItems.irTagName, formItems.irRate
          ],
          operationConfig: {
            fixed: 'left',
            width: 220,
            operationButton: [
              { value: '删除', func: params => this.delete(params) },
              { value: '修改', func: params => this.showDialog('update', params) },
              { value: '新增利率行情', func: params => this.showDialog2('add', params) }
            ]
          },
          operationConfig2: {
            fixed: 'left',
            width: 120,
            operationButton: [
              { value: '修改', func: (params) => this.showDialog2('update', params) },
              { value: '删除', func: params => this.deleteRateInfo(params) }]
          }
        },
        form: {
          rules: {
            ir_tag_name: [{ required: true, message: '指标名称必填', trigger: 'change' }],
            ir_tag_level: [{ required: true, message: '等级必选', trigger: 'change' }],
            ir_tradedate: [{ required: true, message: '生效日期必选', trigger: 'change' }],
            ir_tag_code: [{ required: true, message: '指标编码必填', trigger: 'change' }],
            ir_rate: [{ required: true, message: '利率必填', trigger: 'change' }]
          }
        },
        parmsValue: {
          query: {
            ir_tag_name: '',
            ir_tag_level: ''
          },
          update: {
            ir_tag_name: '',
            ir_tag_level: ''
          },
          update2: {
            ir_tradedate: '',
            ir_tag_code: '',
            ir_tag_name: '',
            ir_rate: '',
            max_ir_rate: ''
          },
          updateOpen: false,
          updateOpen2: false,
          editMod: 'update',
          editMod2: 'update'
        }
      }
    },
    computed: {
      editModStr() {
        switch (this.parmsValue.editMod) {
        case 'update':
          return '修改利率指标'
        case 'add':
          return '新增利率指标'
        case 'detail':
        default:
          return '利率指标详情'
        }
      },
      updateMod() {
        return this.parmsValue.editMod === 'update'
      },
      detailMod() {
        return this.parmsValue.editMod === 'detail'
      },
      addMod() {
        return this.parmsValue.editMod === 'add'
      },
      editModStr2() {
        switch (this.parmsValue.editMod2) {
        case 'update':
          return '修改行情利率'
        case 'add':
          return '新增行情利率'
        case 'detail':
        default:
          return '行情利率详情'
        }
      },
      updateMod2() {
        return this.parmsValue.editMod2 === 'update'
      },
      detailMod2() {
        return this.parmsValue.editMod2 === 'detail'
      },
      addMod2() {
        return this.parmsValue.editMod2 === 'add'
      }
    },
    // 生命周期开始
    activated() {
      this.init()
    },
    // 生命周期结束
    methods: {
      init() {
        const dictReq = []
        const dicts = ['t8_prod_ratelevel']
        dicts.forEach(key => dictReq.push(request({
          url: 'system/base/dict/' + key + '.json',
          method: 'post',
          loading: true
        })))
        axios.all(dictReq).then(axios.spread((ir_tag_level) => {
          this.dict['ir_tag_level'] = ir_tag_level
          this.loadTable()
        }))
      },
      query() {
        this.table.ir1.nowPage = 1
        this.loadTable()
      },
      currentChange(index) {
        console.log(index)
        this.table.ir1.nowPage = index
        this.loadTable()
      },
      currentChange2(index) {
        this.table.ir2.nowPage = index
        this.loadTable()
      },
      loadTable() {
        this.table.ir1.ir_tag_name = this.parmsValue.query.ir_tag_name
        this.table.ir1.ir_tag_level = this.parmsValue.query.ir_tag_level
        kk.k_load_table(this, 'ir1', formItems.VAL)
      },
      loadTable2() {
        this.table.ir2.id = this.table.currentSelect.id
        this.table.ir2.ir_tag_code = this.table.currentSelect.ir_tag_code
        this.table.ir2.ir_tag_name = this.table.currentSelect.ir_tag_name
        kk.k_load_table(this, 'ir2', formItems.VAL)
      },
      showDialog(editMod, params = {}) {
        switch (editMod) {
        case 'add':
          this.parmsValue.editMod = 'add'
          this.parmsValue.updateOpen = true
          break
        case 'update':
          this.parmsValue.editMod = 'update'
          this.parmsValue.updateOpen = true
          this.$nextTick(() => {
            Object.assign(this.parmsValue.update, params)
          })
          break
        case 'detail':
          this.parmsValue.editMod = 'detail'
          this.parmsValue.updateOpen = true
          this.$nextTick(() => {
            Object.assign(this.parmsValue.update, params)
          })
          break
        default:
        }
      },
      showDialog2(editMod, params = {}) {
        switch (editMod) {
        case 'add':
          this.parmsValue.editMod2 = 'add'
          this.parmsValue.updateOpen2 = true
          this.parmsValue.update2.ir_tag_code = params.ir_tag_code
          this.parmsValue.update2.ir_tag_name = params.ir_tag_name
          break
        case 'update':
          this.parmsValue.editMod2 = 'update'
          this.parmsValue.updateOpen2 = true
          this.$nextTick(() => {
            Object.assign(this.parmsValue.update2, params)
          })
          break
        case 'detail':
          this.parmsValue.editMod2 = 'detail'
          this.parmsValue.updateOpen2 = true
          this.$nextTick(() => {
            Object.assign(this.parmsValue.update2, params)
          })
          break
        default:
        }
      },
      handleClose2(done) {
        this.resetFields('updateForm2')
        done()
      },
      closeDialog2() {
        this.parmsValue.updateOpen2 = false
        this.resetFields('updateForm2')
      },
      handleClose(done) {
        this.resetFields('updateForm')
        done()
      },
      closeDialog() {
        this.parmsValue.updateOpen = false
        this.resetFields('updateForm')
      },
      resetFields(form) {
        this.$refs[form].resetFields()
      },
      showSecondTable(params) {
        this.table.currentSelect = params
        this.loadTable2(params)
      },
      /**
       * 更新或新增利率指标
       */
      updateIrTag() {
        this.$refs['updateForm'].validate(valid => {
          if (valid) {
            const exeid = this.addMod ? 'addNormRate01' : 'updateNormRate'
            kk.k_update_exeid(formItems.VAL, exeid, this.parmsValue.update).then(resp => {
              if (resp.success) {
                this.closeDialog()
                this.loadTable()
              }
            })
          }
        })
      },
      /**
       * 删除利率指标
       * @param params
       */
      delete(params) {
        this.$confirm('确认删除利率指标吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          // 先查询是否有利率行情，有则不能删除
          this.queryIrByTag(params.ir_tag_code).then(resp => {
            if (resp.rows.length > 0) {
              this.$message.warning('该指标下已有利率行情，不允许删除')
            } else {
              kk.k_update_exeid(formItems.VAL, 'delNormRate', params).then(resp => {
                if (resp.success) {
                  this.loadTable()
                }
              })
            }
          })
        }).catch(() => {
        })
      },
      /**
       * 查询利率行情
       * @param irTagCode
       * @returns {rep}
       */
      queryIrByTag(irTagCode) {
        return kk.k_load_exeid(formItems.VAL, 'getNormRateInfo', {
          ir_tag_code: irTagCode
        })
      },

      /**
       * 新增利率行情时，是否要与理财对接
       * @param params
       */
      addNewIrInfo(params) {
        const callback = (is_to_t5) => {
          this.parmsValue.update2.opt = 'add'
          // 判断是否与理财对接
          if (is_to_t5 === '0') {
            kk.k_update_exeid(formItems.VAL, 'addNormRateInfo', this.parmsValue.update2).then(resp => {
              if (resp.success) {
                this.closeDialog2()
                this.loadTable2()
              }
            })
          } else {
            kk.k_load_url(formItems.VAL, '/sendProdNR.json', this.parmsValue.update2).then(resp => {
              if (resp.success) {
                this.closeDialog2()
                this.loadTable2()
              }
            })
          }
        }

        this.$refs['updateForm2'].validate(valid => {
          if (valid) {
            kk.getSysParam('80000010', callback)
          }
        })
      },
      /**
       * 更新利率行情
       */
      updateRateInfo() {
        const callback = (is_to_t5) => {
          this.parmsValue.update2.opt = 'upd'
          // 判断是否与理财对接 0 没有对接 1对接
          if (is_to_t5 === '0' || is_to_t5 === 0) {
            kk.k_update_exeid(formItems.VAL, 'updateProdNormRateInfo', this.parmsValue.update2).then(resp => {
              if (resp.success) {
                this.closeDialog2()
                this.loadTable2()
              }
            })
          } else if (is_to_t5 === '1' || is_to_t5 === 1) {
            kk.k_load_url(formItems.VAL, '/sendProdNR.json', this.parmsValue.update2).then(resp => {
              if (resp.success) {
                this.closeDialog2()
                this.loadTable2()
              }
            })
          }
        }

        this.$refs['updateForm2'].validate(valid => {
          if (valid) {
            kk.getSysParam('80000010', callback)
          }
        })
      },
      /**
       * 删除利率行情
       * @param params
       */
      deleteRateInfo(params) {
        this.$confirm('确认删除利率行情吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          kk.k_update_exeid(formItems.VAL, 'delNormRateInfo', params).then(resp => {
            if (resp.success) {
              this.loadTable2()
            }
          })
        }).catch(() => {
        })
      }
    }
  }
</script>
